package com.pangu.ui.browser;

import com.pangu.AMApplication;
import com.pangu.ui.R;
import com.pangu.task.AServiceWrapper;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ItemDecoration;
import android.util.AttributeSet;
import android.view.View;


/**
 * RecyclerView列表容器
 * <p/>
 * Created by cxlin
 */
public abstract class RecyclerViewItemBrowser<A extends AMApplication> extends LoadableView<A> implements
        IResultReceiver {

    public static final String TAG = RecyclerViewItemBrowser.class.getSimpleName();

    protected RecyclerView recyclerView;
    protected RecyclerView.LayoutManager layoutManager;
    protected RecyclerView.Adapter adapter;

    public RecyclerViewItemBrowser(Context context) {
        super(context);
    }

    public RecyclerViewItemBrowser(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected View createContentView() {
        recyclerView = new RecyclerView(getContext());
        recyclerView.setId(R.id.mui__recyclerview_id);

        layoutManager = createLayoutManager();
        recyclerView.setLayoutManager(layoutManager);

        adapter = createAdapter();
        recyclerView.setAdapter(adapter);
        setRecyclerViewParameters(recyclerView);

        return recyclerView;
    }

    /**
     * 设置RecyclerView的风格
     *
     * @param recyclerView
     */
    protected void setRecyclerViewParameters(RecyclerView recyclerView) {
        recyclerView.setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
        recyclerView.addItemDecoration(createItemDecoration());
        recyclerView.setPadding(0, 0, 0, 0);
    }

    /**
     * 创建item项装饰, 设置边距
     *
     * @return
     */
    protected ItemDecoration createItemDecoration() {
        return new RecyclerItemDecoration(getContext(), LinearLayoutManager.HORIZONTAL);
    }

    /**
     * 创建layout管理器
     *
     * @return
     */
    protected abstract RecyclerView.LayoutManager createLayoutManager();

    /**
     * 创建适配器
     *
     * @return
     */
    protected abstract RecyclerView.Adapter createAdapter();

    @Override
    public boolean isLoadDataEmpty() {
        return getDataItemCount() == 0;
    }

    /**
     * 反回这个列表里总共的条数
     * 这里返回的项的数据没有包括头部和尾部
     *
     * @return
     */
    public int getDataItemCount() {
        RecyclerView.Adapter adapter = getMAdapter();
        if (adapter != null) {
            return adapter.getItemCount();

        } else {
            return 0;
        }
    }

    /**
     * 获取适配器
     *
     * @return
     */
    protected RecyclerView.Adapter getMAdapter() {
        if (recyclerView != null) {
            return recyclerView.getAdapter();

        } else {
            return null;
        }
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();

        RecyclerView.Adapter adapter = getMAdapter();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void tryQueryNewItems(int taskType) {
        if (taskType == ATaskMark.TYPE_DEFAULT) {
            tryQueryNewItems(true, taskType);

        } else {
            tryQueryNewItems(false, taskType);
        }
    }

    protected void tryQueryNewItems(boolean showStatus, int taskType) {
        // 先检查是否还需要加载
        // 此处更改解决出现加载失败点击无效果问题
        if ((!mTaskMark.isLoadEnd() || taskType == ATaskMark.TYPE_REFRESH)
                && mTaskMark.getTaskStatus() != ATaskMark.HANDLE_DOING
                && mTaskMark.getTaskStatus() != ATaskMark.HANDLE_ERROR) {
            // 设置加载类型
            mTaskMark.setTaskType(taskType);
            if (mTaskMark.getDependTask() != null) {
                mTaskMark.getDependTask().setTaskType(taskType);
            }
            if (isAutoLoad()) {
                handleLoadNewItems(mTaskMark);

            } else {
                mTaskMark.setTaskStatus(ATaskMark.HANDLE_WAIT);
            }

        } else if (mTaskMark.getTaskStatus() == ATaskMark.HANDLE_DOING) {
            AServiceWrapper.forceTakeoverTask(this, mTaskMark);
        }

        if (showStatus || mTaskMark.getTaskStatus() != ATaskMark.HANDLE_DOING) {
            updateViewStatus(mTaskMark);
        }
    }

    /**
     * 子类处理具体项的加载
     *
     * @param taskMark
     */
    protected abstract void handleLoadNewItems(ATaskMark taskMark);

    /**
     * 添加头部
     *
     * @param header
     */
    protected void addHeader(View header) {
        if (recyclerView.getAdapter() instanceof ARecyclerViewWithHeaderFooterAdapter) {
            ARecyclerViewWithHeaderFooterAdapter adapter = (ARecyclerViewWithHeaderFooterAdapter) recyclerView
                    .getAdapter();
            adapter.addHeader(header);

        }
    }

    /**
     * 移除头部
     *
     * @param header
     */
    protected void removeHeader(View header) {
        if (recyclerView.getAdapter() instanceof ARecyclerViewWithHeaderFooterAdapter) {
            ARecyclerViewWithHeaderFooterAdapter adapter = (ARecyclerViewWithHeaderFooterAdapter) recyclerView
                    .getAdapter();
            adapter.removeHeader(header);

        }
    }

    /**
     * 添加底部
     *
     * @param footer
     */
    protected void addFooter(View footer) {
        if (recyclerView.getAdapter() instanceof ARecyclerViewWithHeaderFooterAdapter) {
            ARecyclerViewWithHeaderFooterAdapter adapter = (ARecyclerViewWithHeaderFooterAdapter) recyclerView
                    .getAdapter();
            adapter.addFooter(footer);

        }
    }

    /**
     * 移除底部
     *
     * @param footer
     */
    protected void removeFooter(View footer) {
        if (recyclerView.getAdapter() instanceof ARecyclerViewWithHeaderFooterAdapter) {
            ARecyclerViewWithHeaderFooterAdapter adapter = (ARecyclerViewWithHeaderFooterAdapter) recyclerView
                    .getAdapter();
            adapter.removeFooter(footer);

        }
    }

}
